package com.prs.crm.action.cdxh;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.cdxh.NewPlanOrder;
import com.prs.crm.domain.cdxh.NewPlanOrderDetail;
import com.prs.crm.domain.cdxh.cd_Product;
import com.prs.crm.service.cdxh.NewPlanOrderService;
import com.prs.crm.service.cdxh.SaleOrderService;

/**
 * 新发生生产计划
 * 
 * @author Administrator
 * 
 */
public class NewPlanOrderAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private NewPlanOrder newPlanOrder;
	
	private SaleOrderService saleOrderService;

	private Collection<NewPlanOrderDetail> newProducts;
	
	private Collection<NewPlanOrderDetail> newslist=new ArrayList<NewPlanOrderDetail>();
	
	private NewPlanOrderService newPlanOrderService;

	private Collection<NewPlanOrderDetail> slist;

	private PaginationSupport pagination;

	private String listString;

	/**
	 * 明细数组
	 */
	private String[] productCodes;
	private String[] pronames;
	private String[] productNorms;
	private String[] productColors;
	private String[] productpackges;
	private String[] productNums;
	private String[] productUnits;
	private String[] poductPrices;
	private String[] prototalPrices;

	private String planOrder;

	/**
	 * 新添加产品计划
	 * 
	 * @return
	 */
	@Action("newProductPlan")
	public String newProductPlan() {
		return SUCCESS;
	}

	/**
	 * 产品列表
	 * 
	 * @return
	 */
	@Action("selectcdProduct")
	public String selectcdProduct() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				cd_Product.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNewPlanOrderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Clew(results = { "success", "none" })
	@Action(value = "saveNewPlanProduct", results = {
			@Result(name = "success", type = "redirect", location = "newProductPlan"),
			@Result(name = "error", type = "chain", location = "newProductPlan") })
	public String saveNewPlanProduct() {
		if (newPlanOrder.getSuppliesTime() == null
				|| newPlanOrder.getSuppliesTime().equals("")) {
			this.addActionError("登记时间不能为空！");
			return ERROR;
		}
		if (productCodes == null || productCodes.length <= 0) {
			this.addActionError("请添加商品！");
			return ERROR;
		}

//		System.out.println(productpackges.length);
		if (productpackges == null || productpackges.length <= 0) {
			this.addActionError("请添加商品包装方式！");
			return ERROR;
		}
		this.newPlanOrderService.saveNewPlan(newPlanOrder, productCodes,
				pronames, productNorms, productColors, productpackges,
				productNums, productUnits, poductPrices, prototalPrices);

		this.setPrompt("新建生产计划建立成功");
		return SUCCESS;
	}

	@Action("viewnewProductPlan")
	public String viewnewProductPlan() {
		newPlanOrder = this.getNewPlanOrderService().get(newPlanOrder.getId());
		this.slist = newPlanOrder.getDetails();

		return SUCCESS;
	}

	@Action("listNewProductPlan")
	public String listNewProductPlan() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				NewPlanOrder.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNewPlanOrderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editnewPlanState")
	public String editnewPlanState() {
//		System.out.println("对象=" + newPlanOrder);
		newPlanOrder = this.getNewPlanOrderService().get(newPlanOrder.getId());
//		System.out.println("之后的对象=" + newPlanOrder);
		planOrder = newPlanOrder.getNewProCode();
//		System.out.println("订单制=" + newPlanOrder.getNewProCode());

		return SUCCESS;
	}

	@Action(value = "saveNewPlanState", results = { @Result(name = "success", type = "chain", location = "editnewPlanState"), })
	public String saveNewPlanState() {
		String state = newPlanOrder.getRushOrder();
		newPlanOrder = this.getNewPlanOrderService().get(newPlanOrder.getId());
		newPlanOrder.setRushOrder(state);
		this.getNewPlanOrderService().saveOrNewPlanOrder(newPlanOrder);
		this.getNewPlanOrderService().setAlllRushOrders(newPlanOrder);
		return SUCCESS;
	}
	@Action("listNewPlanProductState")
	public String listNewPlanProductState() {
		newProducts = newPlanOrder.getDetails();
		String planOrder=newPlanOrder.getNewProCode();
		for(NewPlanOrderDetail sd:newProducts){			
			sd.setStatets(this.getSaleOrderService().setSaleState(planOrder, sd.getProductCode()));
//			System.out.println("zhuangta"+sd.getStatets());
			newslist.add(sd);
		}
		

		return SUCCESS;
	}
	public NewPlanOrder getNewPlanOrder() {
		return newPlanOrder;
	}

	public void setNewPlanOrder(NewPlanOrder newPlanOrder) {
		this.newPlanOrder = newPlanOrder;
	}

	public NewPlanOrderService getNewPlanOrderService() {
		return newPlanOrderService;
	}

	public void setNewPlanOrderService(NewPlanOrderService newPlanOrderService) {
		this.newPlanOrderService = newPlanOrderService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public String getListString() {
		return listString;
	}

	public void setListString(String listString) {
		this.listString = listString;
	}

	public String[] getProductCodes() {
		return productCodes;
	}

	public void setProductCodes(String[] productCodes) {
		this.productCodes = productCodes;
	}

	public String[] getPronames() {
		return pronames;
	}

	public void setPronames(String[] pronames) {
		this.pronames = pronames;
	}

	public String[] getProductNorms() {
		return productNorms;
	}

	public void setProductNorms(String[] productNorms) {
		this.productNorms = productNorms;
	}

	public String[] getProductColors() {
		return productColors;
	}

	public void setProductColors(String[] productColors) {
		this.productColors = productColors;
	}

	public String[] getProductpackges() {
		return productpackges;
	}

	public void setProductpackges(String[] productpackges) {
		this.productpackges = productpackges;
	}

	public String[] getProductNums() {
		return productNums;
	}

	public void setProductNums(String[] productNums) {
		this.productNums = productNums;
	}

	public String[] getProductUnits() {
		return productUnits;
	}

	public void setProductUnits(String[] productUnits) {
		this.productUnits = productUnits;
	}

	public String[] getPoductPrices() {
		return poductPrices;
	}

	public void setPoductPrices(String[] poductPrices) {
		this.poductPrices = poductPrices;
	}

	public String[] getPrototalPrices() {
		return prototalPrices;
	}

	public void setPrototalPrices(String[] prototalPrices) {
		this.prototalPrices = prototalPrices;
	}

	public Collection<NewPlanOrderDetail> getSlist() {
		return slist;
	}

	public void setSlist(Collection<NewPlanOrderDetail> slist) {
		this.slist = slist;
	}

	public String getPlanOrder() {
		return planOrder;
	}

	public void setPlanOrder(String planOrder) {
		this.planOrder = planOrder;
	}

	public Collection<NewPlanOrderDetail> getNewProducts() {
		return newProducts;
	}

	public void setNewProducts(Collection<NewPlanOrderDetail> newProducts) {
		this.newProducts = newProducts;
	}

	public Collection<NewPlanOrderDetail> getNewslist() {
		return newslist;
	}

	public void setNewslist(Collection<NewPlanOrderDetail> newslist) {
		this.newslist = newslist;
	}

	public SaleOrderService getSaleOrderService() {
		return saleOrderService;
	}

	public void setSaleOrderService(SaleOrderService saleOrderService) {
		this.saleOrderService = saleOrderService;
	}


}
